/**
* 借贷管理  利率管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/mainAdmin' }">主页</el-breadcrumb-item>
      <el-breadcrumb-item>借贷管理</el-breadcrumb-item>
      <el-breadcrumb-item>利率管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form class="user-search">
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>

    <!--列表-->
    <el-table size="small"  :data="bankData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" sortable prop="bankRateId" label="银行id" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="bankName" label="银行名字" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="bankRate" label="银行利率" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="dateTime" :formatter="formatDate" label="日期" width="150">
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination class="page-box"   layout="total" :total="pageparm.total">
    </el-pagination>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="银行名" prop="bankName">
          <el-input size="small" v-model="editForm.bankName" auto-complete="off" placeholder="请输入银行名字"></el-input>
        </el-form-item>
        <el-form-item label="银行利率" prop="bankRate">
          <el-input size="small" v-model="editForm.bankRate" auto-complete="off" placeholder="请输入银行利率"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
// 导入请求方法
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: '利率管理',
      editFormVisible: false, //控制编辑页面显示与隐藏
      bankData:[],
      // 编辑与添加
      editForm: {
        bankRateId: '',//银行id
        bankName: '',//银行名字
        bankRate: '',//银行利率
      },
      // rules表单验证
      rules: {
        bankName: [
          { required: true, message: '请输入银行名字', trigger: 'blur' }
        ],
        bankRate: [
          { required: true, message: '请输入银行利率', trigger: 'blur' }
        ],
      },
      // 分页参数
      pageparm: {
        total: 10
      }
    }
  },

  /**
   * 创建完毕
   */
  created() {
    this.getdata()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getdata() {
      this.loading = true
      this.$axios
          .post(`http://localhost:9090/loan/rate/getRateList`).then(
          response => {
            this.bankData = response.data;
            this.userData = response.data;

            // 条数
            this.pageparm.total = this.bankData.length
            this.loading = false
          }
      )
    },

    //日期转换
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if(data == null) {
        return null
      }
      let dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改银行'
        this.editForm.bankRateId = row.bankRateId
        this.editForm.bankName = row.bankName
        this.editForm.bankRate = row.bankRate
      } else {
        this.title = '添加银行'
        this.editForm.bankRateId = ''
        this.editForm.bankName = ''
        this.editForm.bankRate = ''
      }
    },
    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          this.$axios
              .post(`http://localhost:9090/loan/rate/updateRate`,this.$qs.stringify(this.editForm)).then(
              response => {
                this.editFormVisible = false
                this.loading = false
                if (response.data.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '数据保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: "保存失败，请稍后再试！"
                  })
                }

              }
          ).catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('保存失败，请稍后再试！')
          })

        } else {
          return false
        }

      })
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },
    // 删除用户
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            // 删除
            // 请求方法
            this.$axios
                .post(`http://localhost:9090/loan/rate/deleteRate?bankRateId=`+row.bankRateId).then(
                response => {

                  if (response.data.success) {
                    this.getdata(this.formInline)
                    this.$message({
                      type: 'success',
                      message: '数据已删除！'
                    })
                    this.getdata()
                  } else {
                    this.$message({
                      type: 'info',
                      message: "数据删除失败，请稍后再试！"
                    })
                  }

                }
            ).catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })

          })
    },

  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

